// DrawExtension.ts
import { Extension } from '@tiptap/core';
import { createDrawPlugin, createDrawAwareNodeViews, DrawController } from './draw-core';

export const DrawExtension = Extension.create({
    name: 'draw',
    priority: 1000,

    // addOptions() {
    //     return {
    //         hooks: {}, // 可以传入默认 hook 配置
    //     };
    // },

    addProseMirrorPlugins() {
        const { hooks } = this.options;
        const plugin = createDrawPlugin({ hooks });
            console.log('=============');
            
        // 在 Tiptap 里没有直接的 EditorView 引用，这里用一个“懒加载”思路：
        this.editor.once('transaction', ({ editor }) => {
            const view = editor.view;
            const draw = plugin.getState(view.state)?.drawController;
            if (!draw) return;
            this.onCreateDraw?.(draw);
        });

        return [plugin];
    },

    // 可选：在渲染节点时，替换 Tiptap 的默认 nodeViews
    // addNodeView() {
    //     return ({ node, view, getPos, decorations }) => {
    //         // 取出 draw controller
    //         const draw = this.editor.view.state.plugins
    //             .map(p => p.spec.key === drawKey ? drawKey.getState(this.editor.state) : null)
    //             .find(Boolean)?.draw;

    //         const original = this.parent?.();
    //         const proxyFactory = createNodeViewProxy(draw, original);
    //         return proxyFactory(node, view, getPos, decorations);
    //     };
    // },
});